package com.yzj.micro.demo.all.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzj.demo.common.pojo.po.CartPO;
import com.yzj.demo.common.pojo.vo.CartVO;
import com.yzj.micro.demo.all.mapper.CartMapper;
import com.yzj.micro.demo.all.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void saveCart(String userId, String productCode, Integer count) {
        log.info("userId = {}, productCode = {}, count = {}", userId, productCode, count);
        CartPO cartPO = new CartPO(null,productCode,userId,count);
        cartMapper.insert(cartPO);
    }

    @Override
    public List<CartVO> getUserCart(String userId) {
        //使用BaseMapper的现成方法 必须前提是 知道sql怎么写
        //select id,user_id,product_code,count from cart_tbl
        //where user_id=#{userId} and create_time<2025-4-29
        //select查询 主要是告诉持久层mapper where条件
        QueryWrapper<CartPO> whereWrapper=new QueryWrapper<>();
        whereWrapper.eq("user_id",userId);
        //whereWrapper.lt("create_time","2025-4-29");
        //使用这个条件 搜索的是list selectList 搜索的是一个 selectOne
        List<CartPO> list = cartMapper.selectList(whereWrapper);
        if (list!=null && list.size()>0) {
            /*List<CartVO> vos=new ArrayList();
            for (CartPO cartPO : list) {
                CartVO cartVO=new CartVO();
                BeanUtils.copyProperties(cartPO,cartVO);
                vos.add(cartVO);
            }*/
            return list.stream().map(cartPO->{
                CartVO vo=new CartVO();
                BeanUtils.copyProperties(cartPO,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
